package com.otod.pm.prj.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.otod.base.service.BaseService;
import com.otod.commons.auth.SessionUserHolder;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.utils.Collections3;
import com.otod.pm.constants.PmConstant;
import com.otod.pm.prj.mapper.TeamMapper;
import com.otod.pm.prj.vo.PmTaskEmpVo;
import com.otod.pm.prj.vo.TeamVo;
import com.otod.sys.emp.service.IEmpService;

/**
 * 员工信息管理
 * @author jqiu
 */
@Service
@Transactional(readOnly=true)
public class TeamService extends BaseService {
	
	@Autowired
	TeamMapper  teamMapper;
	
	@Autowired
	private PmTaskEmpService taskEmpService;
	
	@Autowired
	private IEmpService empService;
	
	/**
	 * 新增项目人员
	 */
	@Transactional
	public void addTeam(TeamVo vo) {
		vo.setUuid(super.getUUID());
		teamMapper.insert(vo);
	}
	
	/**
	 * 删除成员
	 */
	@Transactional
	public void deleteByprjId(String prjId) {
		
		teamMapper.deleteById(prjId);
	}
	
	
	/**
	 * 通过UUID删除成员
	 */
	@Transactional
	public String deleteByUuid(String uuid) {
		TeamVo teamVo = teamMapper.getById(uuid);
		String prjId= teamVo.getPrjId();
		String empNo = teamVo.getEmpNo();
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("prjId", prjId);
		param.put("empNo", empNo);
		if(Collections3.isNotEmpty(taskEmpService.queryForList(param))){
			return "该人员在当前项目中拥有主导或辅助的任务，不能删除";
		}
		String roleNo = teamVo.getRoleNo();
		Integer number = 0;
		if("30100002".equals(roleNo)){
			List<TeamVo> list =  this.findByPrjId(prjId);
			for (TeamVo vo : list) {
				if("30100002".equals(vo.getRoleNo())){
					number++;
				}
			}
		}
		if(number == 1){
			return "不能删除，项目必须有一个项目负责人！";
		}
		
		teamMapper.deleteByUuid(uuid);
		return "删除成功";
	}
	
	/**
	 * 更新项目
	 */
	@Transactional
	public void updateTeam(Map<String,Object> map) {
		teamMapper.update(map);
	}
	
	/**
	 * 查询项目人员List
	 * param  map
	 */
	public List<TeamVo> findByPrjId(String prjId) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("prjId", prjId);
		List<TeamVo> teamList = teamMapper.queryForList(param);
		String roleNo;
		for (TeamVo teamVo : teamList) {
			roleNo = teamVo.getRoleNo();
			if(PmConstant.PRJ_ROLE_MANAGER.equals(roleNo)){
				teamVo.setRoleName("项目经理");
			}else
			if(PmConstant.PRJ_ROLE_MEMBER.equals(roleNo)){
				teamVo.setRoleName("项目成员");
			}else
			if(PmConstant.PRJ_ROLE_PRINCIPAL.equals(roleNo)){
				teamVo.setRoleName("项目负责人");
			}else
			if(PmConstant.PRJ_ROLE_CHARGE.equals(roleNo)){
				teamVo.setRoleName("工作室主任");
			}else
			if(PmConstant.PRJ_ROLE_ENTIRE.equals(roleNo)){
				teamVo.setRoleName("项目总师");
			}else
			if(PmConstant.PRJ_ROLE_CHECK.equals(roleNo)){
				teamVo.setRoleName("项目校对");
			}else
			if(PmConstant.PRJ_ROLE_ADMIN.equals(roleNo)){
				teamVo.setRoleName("项目管理员");
			}
		}
		return teamList;
	}
	
	/**
	 * 查询项目人员List
	 * param  map
	 */
	public List<TeamVo> findByPrjIdAndRoleNo(String prjId,String roleNo) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("prjId", prjId);
		param.put("roleNo", roleNo);
		List<TeamVo> teamList = teamMapper.queryForList(param);
		
		for (TeamVo teamVo : teamList) {
			roleNo = teamVo.getRoleNo();
			if(PmConstant.PRJ_ROLE_MANAGER.equals(roleNo)){
				teamVo.setRoleName("项目经理");
			}else
				if(PmConstant.PRJ_ROLE_MEMBER.equals(roleNo)){
					teamVo.setRoleName("项目成员");
				}else
					if(PmConstant.PRJ_ROLE_PRINCIPAL.equals(roleNo)){
						teamVo.setRoleName("项目负责人");
					}else
						if(PmConstant.PRJ_ROLE_CHARGE.equals(roleNo)){
							teamVo.setRoleName("工作室主任");
						}else
							if(PmConstant.PRJ_ROLE_ENTIRE.equals(roleNo)){
								teamVo.setRoleName("项目总师");
							}else
								if(PmConstant.PRJ_ROLE_CHECK.equals(roleNo)){
									teamVo.setRoleName("项目校对");
								}else
									if(PmConstant.PRJ_ROLE_ADMIN.equals(roleNo)){
										teamVo.setRoleName("项目管理员");
									}
		}
		return teamList;
	}
	
	/**
	 * 查询登陆人员所在的团队
	 * @param empNo  登录人员的empNo
	 * @return
	 * @author longjiweni
	 */
	public List<TeamVo> queryEmpNo(String empNo){
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("empNo", empNo);
		return teamMapper.queryForList(param);
	}
	
	/**
	 * 查询人员是否已存在
	 * @param empNo  prjId
	 * @return
	 * @author yichen
	 */
	public List<TeamVo> queryByEmpNoAndPrjId(String empNos,String prjId){
		Map<String, Object> param = new HashMap<String,Object>();
		List<TeamVo> list = new ArrayList<TeamVo>();
		if(!StringUtils.isEmpty(empNos)){
			String[] empNoList = empNos.split(",");
			for (String empNo : empNoList) {
				param.put("prjId", prjId);
				param.put("empNo", empNo);
				list = teamMapper.queryForList(param);
				if(Collections3.isNotEmpty(list)){
					return list;
				}
				list.clear();
			}
		}
		return list;
	}
	
	/**
	 * 获取人员在项目中的角色（如果人员未在项目中则返回空字符串）
	 * @param prjId		项目ID
	 * @param userNo	人员编号
	 * @return
	 */
	public String getRoleInTeam(String prjId,String userNo){
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("empNo", userNo);
		param.put("prjId", prjId);
		List<TeamVo>  list = teamMapper.queryForList(param);
		if(Collections3.isEmpty(list)){
			return "";
		}
		String roleNo = list.get(0).getRoleNo();
		
		return roleNo==null?"":roleNo;
	}
	/**
	 * 获取项目中的负责人（如果人员未在项目中则返回空字符串）
	 * @param prjId		项目ID
	 * @param roleNo	30100002
	 * @return
	 */
	public String getManagerNameInPrj(String prjId,String roleNo){
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("roleNo", roleNo);
		param.put("prjId", prjId);
		List<TeamVo>  list = teamMapper.queryForList(param);
		if(Collections3.isEmpty(list)){
			return "";
		}else{
			String empNo = list.get(0).getEmpNo();
			String empName =  empService.findByEmpNo(empNo).getEmpName();
			
			return empName==null?"":empName;
		}
		
		
	}
	
	/**
	 * @deprecated
	 * 设置当前的用户在奔项目中的权限
	 * @param 
	 * @return 
	 */
	public String getAuth(String prjId){
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		String userNo = sessionUser.getUserNo();
		String roleNo = getRoleInTeam(prjId, userNo);
		StringBuffer sb = new StringBuffer();
		if(PmConstant.PRJ_ROLE_MANAGER.equals(roleNo)){  //项目经理
			 sb.append(PmConstant.BUTTON_ADD)
			 	.append(";").append(PmConstant.BUTTON_DELETE)
			 	.append(";").append(PmConstant.BUTTON_EDIT)
			 	.append(";").append(PmConstant.BUTTON_VIEW);
			 
			 return sb.toString();
		}else if(PmConstant.PRJ_ROLE_MEMBER.equals(roleNo)){ // 成员
			
			sb.append(";").append(PmConstant.BUTTON_VIEW);
			
			return sb.toString();
		}else if(PmConstant.PRJ_ROLE_PRINCIPAL.equals(roleNo)){ //负责人
			sb.append(PmConstant.BUTTON_ADD)
		 	.append(";").append(PmConstant.BUTTON_DELETE)
		 	.append(";").append(PmConstant.BUTTON_EDIT)
		 	.append(";").append(PmConstant.BUTTON_VIEW);
			
			return sb.toString();
		}
		return "";
	}
	
	/**
	 * 逻辑删除 项目团队人员管理
	 * @param map
	 */
	@Transactional
	public void remove(Map<String, Object> map) {
		teamMapper.remove(map);
	}
	
	/**
	 * 查询信息列表  
	 * @param param	参数
	 * @return	分页信息和结果数据
	 */
	public List<TeamVo> queryForList(Map<String,Object> param){
		List<TeamVo> dataList =  teamMapper.queryForList(param);
		return dataList;
	}
}
